<?php
include 'conn_mysql.php';
session_start();
$phone          =       $_SESSION['phone'];

$code			=		$_REQUEST['code'];
$other_people 	=		$_REQUEST['state'];//分享者ID

//通过code换取网页授权access_token
$url="https://api.weixin.qq.com/sns/oauth2/access_token";
$data['appid']=$APPID;
$data['secret']=$APPSECRET;
$data['code']=$code;
$data['grant_type']="authorization_code";
//定义返回值接收变量；
$httpstr =http($url, $data, 'GET');
$de_json=json_decode($httpstr,true);

$open_id 	    = 	$de_json['openid'];
$access_token 	= 	$de_json['access_token'];
$refresh_token  =   $de_json['refresh_token'];

echo $open_id;
exit;

//刷新access_token
$url="https://api.weixin.qq.com/sns/oauth2/refresh_token";
$data['appid']=$APPID;;
$data['grant_type']="refresh_token";
$data['refresh_token']=$refresh_token;
//定义返回值接收变量；
$httpstr =http($url, $data, 'GET');
$de_json=json_decode($httpstr,true);

$access_token 	= 	$de_json['access_token'];
$refresh_token  =   $de_json['refresh_token'];

//利用access_token 获取用户信息
$url="https://api.weixin.qq.com/sns/userinfo";
$data['access_token']=$access_token;
$data['openid']=$open_id;
$data['lang']="zh_CN";
//定义返回值接收变量
$httpstr =http($url, $data, 'GET');
$de_json=json_decode($httpstr,true);

$headimgurl 	= 	$de_json['headimgurl'];//用户头像
$nickname       =   $de_json['nickname'];//用户昵称

//将用户的openid，头像，昵称保存入数据库
$sql="SELECT * FROM `admin_user` WHERE  open_id='".$open_id."'";
$query = mysql_query($sql);
if(mysql_num_rows($query)<1){
	$sql="INSERT INTO `admin_user`(`open_id`,`wx_nicheng`, `wx_touxiang`) VALUES ('$open_id','$nickname','$headimgurl')";
}else{
	$sql="UPDATE `admin_user` SET `wx_nicheng`='$nickname',`wx_touxiang`='$headimgurl' WHERE open_id='$open_id'";
}
$query = mysql_query($sql);

function http($url,  $params, $method = 'GET', $header = array(), $multi = false){
	//var_dump($params);
	//echo json_encode($params);
    $opts = array(
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_HTTPHEADER     => $header
    );
    /* 根据请求类型设置特定参数 */
    switch(strtoupper($method)){
        case 'GET':
            $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
            break;
        case 'POST':
            //判断是否传输文件
            $params = $multi ? $params : http_build_query($params);
            $opts[CURLOPT_URL] = $url;
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $params;
            break;
        default:
            throw new Exception('不支持的请求方式！');
    }
    /* 初始化并执行curl请求 */
    $ch = curl_init();
    curl_setopt_array($ch, $opts);
    $data  = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    if($error) throw new Exception('请求发生错误：' . $error);
    return  $data;
}

//用户信息
$open_id     =  $open_id;        
$headimgurl  =  $headimgurl;  //头像
$nickname    =  $nickname;    //昵称 
$sql="SELECT * FROM `admin_user` WHERE open_id='$open_id'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)){
	$user_name=$row['user_name'];
	$phone=$row['phone'];    //电话
	$lvbi=$row['lvbi'];      //当前持有驴币数
	if(strlen($user_name)==0){
		//如果用户姓名为空，则用微信昵称代替
		$is_have_name=0; //用户是否已绑定姓名标识符
		$user_name=$nickname;
	}
}
?>
<head>
<meta name="format-detection" content="telephone=no">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<script language="javascript" src="js/jquery.min182.js"></script>
<script language="javascript" src="js/jquery.form.js"></script>
<script language="javascript" src="js/json.parse.js"></script>
<script language="javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<title>驴行天下个人中心</title>
<SCRIPT LANGUAGE="javascript"> 
	function adaptive(){
		var wight=document.body.clientWidth;
		document.getElementById("top_head").style.height = wight/711*444+"px";
		
		document.getElementById("body").style.display="";
	}
</SCRIPT>
</SCRIPT>
</head>
<body style="margin:0px;max-width:640px;min-height:550px;margin-right:auto;margin-left:auto;background-color:#2D394E;" id="body" onLoad="adaptive()">
	<div id="top_head" style="width:100%;height:260px;background:url(picture/id_difang_1_3.png) no-repeat ;background-size: 100% 100%;position:relative;">
		<img src="picture/15.png" id="head_bottom" style="width:100%;height:70px;float:left;position:absolute;bottom:-1px;"></img>
		
		<div id="head_bottom1" style="width:100%;height:70px;float:left;position:absolute;bottom:-1px;">
			<img src="<?php echo $headimgurl;?>" style="float:left;display:block;width:50px;height:50px;float:left;margin-left:3%;" id="touxiang">
			<div style="float:left;margin-top:8px;margin-left:3%;">
				<span style="color:#FFFFFF;font-size:16px;"><?php echo $user_name;?></span><br/>
				<span style="color:#FFFFFF;font-size:16px;">
				<?php if(strlen($phone)<6){echo "未绑定手机号";}else{echo $phone;}?>
				</span>
			</div>
			<a style="float:right;color:#FFFFFF;font-size:16px;display:block;margin-top:25px;margin-right:3%;">驴币：<?php echo $lvbi;?></a>
		</div>
	</div>
	
	<div style="width:84%;height:34px;padding: 6px 3%;background-color:transparent;font-size:18px;font-family:微软雅黑;color:white;border: 1px solid #fff;border-radius: 6px;margin: 15px 5% 0 5%;">
		<div style="float:left;">
			<a href=""><img src="picture/zl.png" style="width:25px;height:25px;margin-top:4.5px;"></img></a>
		</div>
		<div style="width:83%;float:left;margin-left:5%;margin-top:4.5px;">
			<span >我的资料</span>
			<span style="color:#ccc;float:right;" onClick="location.href='xiugai_admin_user_info.php?open_id=<?php echo $open_id;?>'">继续完善</span>
		</div>
	</div>
	
	<div style="width: 84%;height: 34px;padding: 6px 3%;background-color:transparent;font-size:18px;font-family:微软雅黑;color: white;border: 1px solid #fff;border-radius: 6px;margin: 10px 5% 0 5%;">
		<div style="float:left;">
			<a href=""><img src="picture/jsb.png" style="width:25px;height:25px;margin-top:4.5px;"></a>
		</div>
		<div style="width:83%;float:left;margin-left:5%;margin-top:4.5px;" onClick="location.href=''">
			<span >我的订单</span>
			<span style="color:#ccc;float:right;"><?php echo $order_name; ?>份订单</span>
		</div>
	</div>
	
	<div style="width:100%;height:50px;"></div>
	<a href="lvyou_list.php?phone=<?php echo $phone;?>"><input style="width:100%;height:50px;background-color:#ff0000;line-height:50px;color:#FFFFFF;font-family:微软雅黑;font-size:20px;border:0; position:fixed;z-index:998;bottom:0px;text-align:center" value="驴行列表" onFocus="this.blur()"></input></a>
</body>






















